package com.black.app.service.impl.exercise;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.AppUserLevel;
import com.black.app.domain.exercise.AppExerciseAction;
import com.black.app.domain.exercise.AppExerciseGroup;
import com.black.app.domain.exercise.AppExerciseSeries;
import com.black.app.domain.req.exercise.add.AppExerciseActionAddReq;
import com.black.app.domain.req.exercise.edit.AppExerciseActionEditReq;
import com.black.app.domain.req.exercise.query.AppExerciseActionQueryReq;
import com.black.app.domain.resp.exercise.AdminExerciseActionGroupResp;
import com.black.app.domain.resp.exercise.AdminExerciseActionResp;
import com.black.app.domain.resp.exercise.AppExerciseActionGroupTreeResp;
import com.black.app.domain.resp.exercise.AppExerciseActionResp;
import com.black.app.service.AppUserLevelService;
import com.black.app.service.exercise.AppExerciseActionService;
import com.black.app.mapper.exercise.AppExerciseActionMapper;
import com.black.app.service.exercise.AppExerciseGroupService;
import com.black.app.service.exercise.AppExerciseSeriesService;
import com.black.common.exception.ServiceException;
import com.black.common.utils.MessageUtils;
import com.black.common.utils.OSSUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author L
 * @description 针对表【app_exercise_action(锻炼系列的各个视频)】的数据库操作Service实现
 * @createDate 2024-10-23 16:57:08
 */
@Service
public class AppExerciseActionServiceImpl extends ServiceImpl<AppExerciseActionMapper, AppExerciseAction>
        implements AppExerciseActionService {

    @Autowired
    private AppExerciseSeriesService exerciseSeriesService;

    @Lazy
    @Autowired
    private AppExerciseGroupService exerciseGroupService;

    @Autowired
    private AppUserLevelService appUserLevelService;

    /**
     * 根据系列id获取对应的锻炼
     *
     * @param sId
     * @return
     */
    @Override
    public AppExerciseActionResp findBySId(Long sId) {

        AppExerciseActionResp resp = new AppExerciseActionResp();

        // 根据系列id查询对应的锻炼系列
        AppExerciseSeries appExerciseSeries = this.exerciseSeriesService.findById(sId);

        resp.setSeries(appExerciseSeries);

        List<AppExerciseGroup> groupList = this.exerciseGroupService.findBySId(sId);

        List<AppExerciseActionGroupTreeResp> groupTree = new ArrayList<>();

        groupList.forEach(group -> {
            AppExerciseActionGroupTreeResp groupResp = new AppExerciseActionGroupTreeResp();
            groupResp.setGroup(group);
            List<AppExerciseAction> listByGId = this.baseMapper.findByGId(group.getGroupId());
            if (listByGId.size() != 0) {
                groupResp.setActionList(listByGId);
                groupTree.add(groupResp);
            }
        });

        resp.setGroupList(groupTree);

        return resp;
    }

    /**
     * 根据动作id查询动作
     *
     * @param id
     * @return
     */
    @Override
    public AdminExerciseActionResp findById(Long id) {
        AdminExerciseActionResp resp = this.baseMapper.findById(id);
        return resp;
    }

    /**
     * 根据系列id查询动作
     *
     * @param req
     * @return
     */
    @Override
    public List<AdminExerciseActionGroupResp> getBySId(AppExerciseActionQueryReq req) {
        // 获取该系列的所有动作组
        List<AppExerciseGroup> groupList = this.exerciseGroupService.findBySId(req.getSeriesId());

        List<AdminExerciseActionGroupResp> resp = new ArrayList<>();

        for (AppExerciseGroup item : groupList) {
            List<AdminExerciseActionResp> actionList = this.baseMapper.getByGId(item.getGroupId());
            AdminExerciseActionGroupResp group = new AdminExerciseActionGroupResp();
            group.setGroupRepeats(item.getRepeats());
            group.setSeriesId(item.getSId());
            group.setActionList(actionList);
            group.setGroupNum(item.getGroupNum());
            group.setGroupId(item.getGroupId());
            resp.add(group);
        }

        return resp;
    }

    /**
     * 添加动作
     *
     * @param req
     * @return
     */
    @Override
    public boolean add(AppExerciseActionAddReq req) {
        // 校验 当前分组下是否已存在该动作名称
        AppExerciseAction action = this.baseMapper.findByGIdAndName(req.getGid(), req.getName());
        if (Objects.nonNull(action)) {
            // 删除新上传的图片
            if (req.getImgPath().length() > 0) {
                OSSUtils.deleteFile(req.getImgPath());
            }
            throw new ServiceException(MessageUtils.message("exerciseaction.name.exists"));
        }

        // 校验  动作所属系列是否存在
        AppExerciseSeries series = this.exerciseSeriesService.findById(req.getSid());
        if (Objects.isNull(series)) {
            // 删除新上传的图片
            if (req.getImgPath().length() > 0) {
                OSSUtils.deleteFile(req.getImgPath());
            }
            throw new ServiceException(MessageUtils.message("exerciseseries.not.exists"));
        }

        action = new AppExerciseAction();
        action.setName(req.getName());
        action.setGId(req.getGid());
        action.setTime(req.getTime());
        action.setRepeats(req.getRepeats());
        action.setImgPath(req.getImgPath());
        action.setVideoPath(req.getVideoPath());
        action.setUserLevelId(req.getLevel());
        if (action.getImgPath().length() == 0) {
            action.setImgPath(series.getPath());
        }
        // 校验 会员等级是否存在
        AppUserLevel level = this.appUserLevelService.findById(req.getLevel());
        if (Objects.isNull(level)) {
            // 默认普通会员
            action.setUserLevelId(1);
        }

        return this.save(action);
    }

    /**
     * 编辑动作
     *
     * @param req
     * @return
     */
    @Override
    public boolean edit(AppExerciseActionEditReq req) {
        // 校验  动作所属系列是否存在
        AppExerciseSeries series = this.exerciseSeriesService.findById(req.getSid());
        if (Objects.isNull(series)) {
            // 删除新上传的图片
            if (req.getImgPath().length() > 0) {
                OSSUtils.deleteFile(req.getImgPath());
            }
            throw new ServiceException(MessageUtils.message("exerciseseries.not.exists"));
        }

        // 校验 当前系列下是否存在该分组
        AppExerciseGroup group = this.exerciseGroupService.findBySidAndGid(series.getSeriesId(), req.getGid());
        if (Objects.isNull(group)) {
            // 删除新上传的图片
            if (req.getImgPath().length() > 0) {
                OSSUtils.deleteFile(req.getImgPath());
            }
            throw new ServiceException(MessageUtils.message("exercisegroup.not.exists"));
        }

        // 校验  动作是否存在
        AppExerciseAction action = this.getById(req.getActionId());
        if (Objects.isNull(action)) {
            // 删除新上传的图片
            if (req.getImgPath().length() > 0) {
                OSSUtils.deleteFile(req.getImgPath());
            }
            throw new ServiceException(MessageUtils.message("exerciseaction.not.exists"));
        }
        action.setName(req.getName());
        action.setTime(req.getTime());
        action.setImgPath(req.getImgPath());
        action.setRepeats(req.getRepeats());
        action.setVideoPath(req.getVideoPath());
        action.setUserLevelId(req.getLevel());
        if (action.getImgPath().length() == 0) {
            action.setImgPath(series.getPath());
        }

        return this.updateById(action);
    }

    /**
     * 根据动作组id查询动作
     *
     * @param groupId
     * @return
     */
    @Override
    public List<AppExerciseAction> findByGId(Long groupId) {
        return this.baseMapper.findByGId(groupId);
    }
}




